<template>
  <div>
    <h1>mycom2</h1>
    <h2>{{ msg }}</h2>
    <div>
      <button style="margin-right:10px;" @click="fnChangeName('Ann')">改姓名</button>
      <button @click="fnChangeAge">改年龄</button>
    </div>
  </div>
</template>

<script>
import pubsub from 'pubsub-js'
export default {
  name: 'mycom2',
  data() {
    return {
      msg: 'MSG',
      pubId: null
    }
  },
  methods: {
    getToy() {
      this.msg = Math.random()
    },
    fnChangeName(val) {
      this.$store.commit('CHANGENAME', Date.now())
    },
    fnChangeAge(val) {
      this.$store.dispatch('fnChangeAgeAsync', Date.now())
    },
    getValue(_, value) {
      console.log(value)
    }
  },
  mounted() {
    this.$bus.$on('getToy', this.getToy)
    this.pubId = pubsub.subscribe('getValue', this.getValue)
  },
  beforeDestroy() {
    this.$bus.$off('getToy')
    pubsub.unsubscribe(this.pubId)
  }
}
</script>
